package com.jsmy.marketing.fg;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.activity.PlanDetailActivity;
import com.jsmy.marketing.adapter.PlantFragmentAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.YWgtjhBean;
import com.jsmy.marketing.common.Link;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by PC_WLT on 2018/8/27.
 */

public class PlantFragment extends BaseFragment {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    PlantFragmentAdapter adapter;
    List<YWgtjhBean.DataBean.ListBean> list;
    int pageindex = 1;

    @Override
    protected int resLayout() {
        return R.layout.fg_plant_layout;
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);   adapter = new PlantFragmentAdapter();
        myRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycler.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
                requestData();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                requestData();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                YWgtjhBean.DataBean.ListBean listBean= (YWgtjhBean.DataBean.ListBean) adapter.getItem(position);
                Bundle b= new Bundle();
                b.putString("planid",listBean.getPlanid());
                openActivity(PlanDetailActivity.class,b);
            }
        });
        update();
    }

    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("pageindex", "" + pageindex);
        mPresenter.loadData(RetrofitManager.getInstance().getgtjhlist(map), Link.APP_YW_GTJH_LIST);
    }

    @Override
    public void update(){
        refreshLayout.autoRefresh();
    }
    @Override
    public void refreshData(Object object, String position) {
        switch (position) {
            case Link.APP_YW_GTJH_LIST:
                list = ((YWgtjhBean) object).getData().getList();
                if (pageindex == 1) {
                    refreshLayout.finishRefresh();
                    adapter.replaceData(list);
                } else {
                    refreshLayout.finishLoadmore();
                    adapter.addData(list);
                }
                break;
        }
    }
}
